package cn.wokoba.dao.dao;

import cn.wokoba.dao.dao.po.GroupBuyOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface IGroupBuyOrderDao extends BaseMapper<GroupBuyOrder> {


    @Update("""
                        update group_buy_order
                        set lock_count = lock_count + 1, update_time= now()
                        where team_id = #{teamId} and lock_count < target_count
            """)
    int updateAddLockCount(String teamId);

    int updateSubtractionLockCount(String teamId);

    @Select("      select target_count, complete_count, lock_count from group_buy_order\n" +
            "        where team_id = #{teamId}")
    GroupBuyOrder queryGroupBuyProgress(String teamId);

    @Update("""
                 update group_buy_order
                    set status = 1, update_time= now()
                    where team_id = #{teamId} and status = 0\
            """)
    int updateOrderStatus2COMPLETE(String teamId);

    @Update("""
             update group_buy_order
                        set complete_count = complete_count + 1, update_time= now()
                        where team_id = #{teamId} and complete_count < target_count\
            """)
    int updateAddCompleteCount(String teamId);

    @Update("""
            <script>
                 update group_buy_order set status = 2 ,update_time=now()
                 where team_id in\s
                 <foreach item="teamId" collection="teamIds" open="(" separator="," close=")">
                     #{teamId}
                 </foreach>
             </script>
            """)
    void updateOrderStatus2Fail(@Param("teamIds") List<String> teamIds);

    @Update("  update group_buy_order set status = 2 ,update_time=now() where team_id=#{teamId}")
    void updateOrderStatus2Fail(String teamId);
}
